#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
First time trying to use dynamic ram
*/
int cnt=0;
char** strsplit(const char* str,char det)
{
    char* elels=NULL;
    const char* ele_start=str;
    const char* p=str;
    int i,cap=101;
    char** ele=(char**)malloc(cap*sizeof(char*));
    if(ele==NULL)
    {
        perror("malloc");
        exit(1);
    }
    cnt=0;
    while(*p!='\0')
    {
        if(*p==det)
        {
            elels=(char*)malloc((p-ele_start+1)*sizeof(char*));
            if(elels==NULL)
            {
                perror("malloc");
                for(i=0;i<=cap;i++)
                free(ele[i]);
                free(elels);
                exit(1);
            }
            strncpy(elels,ele_start,p-ele_start);
            elels[p-ele_start]='\0';
            ele[cnt++]=elels;
            ele_start=p+1;
        }
        p++;
    }
    if(ele_start<p)
    {
        elels=(char*)malloc((p-ele_start+1)*sizeof(char));
        if(elels==NULL)
        {
            perror("malloc");
            for(i=0;i<=cnt;i++)
            free(ele[i]);
            free(elels);
            exit(1);
        }
        strncpy(elels,ele_start,p-ele_start);
        ele[cnt++]=elels;
    }
    return ele;
}
int compare_strs(const void* a,const void* b)
{
    const char** strA=(const char**)a;
    const char** strB=(const char**)b;
    return strcmp(*strA,*strB);
}
int main(){
    int i;
    size_t len=0;
    size_t read;
    char** ans_ele;
    char *line=(char*)malloc(101101*sizeof(char));
    if(line==NULL)
    {
        perror("malloc");
        return 1;
    }
    if(fgets(line,sizeof(line),stdin)==NULL)
    {
        perror("fgets");
        return 1;
    }
    len=strlen(line);
    printf("%s\n",line);
    ans_ele=strsplit(line,',');
    qsort(ans_ele,cnt,sizeof(ans_ele[0]),compare_strs);
    for(i=0;i<cnt;i++)
    printf("%s\n",ans_ele[i]);
    // printf("%s",line);
    for(i=0;i<cnt;i++)
    free(ans_ele[i]);
    free(ans_ele);
    free(line);
    return 0;
}